<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-IsShipPortLine__isshipportline">
      <el-form
        :inline="true"
        :model="dataForm"
        ref="forms"
        @keyup.enter.native="getDatsaList()"
        :rules="dataRule"
      >
        <el-form-item>
          <el-select
            v-model="dataForm.startPortNo"
            placeholder="出发港口"
            clearable
          >
            <el-option
              v-for="(item, index) in startOptions"
              :key="index"
              :value="item.portNum"
              :label="item.portName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="dataForm.endPortNo"
            placeholder="到达港口"
            clearable
          >
            <el-option
              v-for="(item, index) in endOptions"
              :key="index"
              :value="item.portNum"
              :label="item.portName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="dataForm.startDate"
            type="date"
            placeholder="开船日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="queryValidate()">{{ $t("query") }}</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        style="width: 100%"
      >
        <el-table-column
          prop="lineNum"
          label="航线号"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="lineNo"
          label="航次"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="shipName"
          label="船名"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="startPortName"
          label="起始港名"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="endPortName"
          label="停靠港名"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="sailDate"
          label="开船日期"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="sailTime"
          label="开船时间"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="busStartTime"
          label="开车时间"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="sx"
          label="时序"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="embarkPortName"
          label="上船码头"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="timeMemosStr"
          label="预计到达时间"
          header-align="center"
          align="center"
          width="170"
        ></el-table-column>
        <el-table-column
          prop="clxm"
          label="船类型名"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="hxlxm"
          label="航线类型名"
          header-align="center"
          align="center"
        ></el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import debounce from "lodash/debounce";
import mixinViewModule from "@/mixins/view-module";
let now = new Date();
let year = now.getFullYear();
let month = String(now.getMonth() + 1).padStart(2, "0");
const day = String(now.getDate()).padStart(2, "0");
const formattedDate = `${year}-${month}-${day}`;
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      startOptions: [],
      endOptions: [],
      mixinViewModuleOptions: {
        getDataListURL: "/travel/isshipport/pageLineTime",
        getDataListIsPage: true,
      },
      dataForm: {
        startPortNo: "1028",
        endPortNo: "1010",
        startDate: formattedDate,
      },
    };
  },
  computed: {
    dataRule() {
      return {
        startPortNo: [
          {
            required: true,
            message: "请选择出发港口",
            trigger: "change",
          },
        ],
        endPortNo: [
          {
            required: true,
            message: "请选择到达港口",
            trigger: "change",
          },
        ],
        startDate: [
          {
            required: true,
            message: "请选择开船日期",
            trigger: "change",
          },
        ],
      };
    },
  },
  created() {
    Promise.all([this.queryOption(0), this.queryOption(1)]).then(() => {
      this.dataForm.startPortNo = '1028';
      this.dataForm.endPortNo = '1010';
    });
  },
  watch: {},
  methods: {
    queryOption(type) {
      this.$http
        .get("/travel/isshipport/listPort", {
          params: {
            type: type,
          },
        })
        .then(({ data: res }) => {
          switch (type) {
            case 0:
              this.startOptions = res.data;
              break;
            case 1:
              this.endOptions = res.data;
              break;
          }
        });
    },
    queryValidate() {
      if (!this.dataForm.startPortNo) {
        this.$message({
          type: "warning",
          message: "出发港口不能为空",
        });
        return;
      }
      if (!this.dataForm.startPortNo) {
        this.$message({
          type: "warning",
          message: "到达港口不能为空",
        });
        return;
      }
      if (!this.dataForm.startPortNo) {
        this.$message({
          type: "warning",
          message: "开船时间不能为空",
        });
        return;
      }
      this.dataListLoading = true;
      this.$http
        .get(this.mixinViewModuleOptions.getDataListURL, {
          params: {
            page: this.mixinViewModuleOptions.getDataListIsPage
              ? this.page
              : null,
            limit: this.mixinViewModuleOptions.getDataListIsPage
              ? this.limit
              : null,
            ...this.dataForm,
          },
        })
        .then(({ data: res }) => {
          this.dataListLoading = false;
          if (res.code !== 0) {
            this.dataList = [];
            this.total = 0;
            return this.$message.error(res.msg);
          }
          this.dataList = this.mixinViewModuleOptions.getDataListIsPage
            ? res.data.list
            : res.data;
          this.total = this.mixinViewModuleOptions.getDataListIsPage
            ? res.data.total
            : 0;
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    },
  },
};
</script>
